package com.jiang.controller;

import com.jiang.dao.userimpl;
import com.jiang.entity.Cart;
import com.jiang.entity.User;
import com.jiang.service.Administratorimpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author jiangjingbo
 * @version V1.0
 * @Project java2203
 * @Package com.jiang.controller
 * @Description:
 * @Date 2022/5/25 20:44
 */
@WebServlet("/cart")
public class CartController  extends BaseServlet {

        public String create(HttpServletRequest request, HttpServletResponse response) {
                //1. 获取商品id         pid是商品id
            String pid = request.getParameter("pid");
             //2. 获取session会话中当前用户的用户id
            User user = (User) request.getSession().getAttribute("loginUser");
            int uid = user.getId();
            if(user==null){
                //如果获取不到用户对象, 说明用户没有登录, 跳转到登录页面
                return "redirect:/login.jsp";
            }
                //3. 调用service业务层, 将购物车存入到数据库中
            Administratorimpl administratorimpl = new Administratorimpl(new userimpl());
            administratorimpl.findCidPid(Integer.parseInt(pid),uid);
            //4. 跳转到购物车查询列表方法
            return  "redirect:/cartSuccess.jsp";
        }//cart?method=Cartshow
        /**
         * 查询指定用户的购物车数据, 并跳转到购物车列表页面展示
         * @return
         */
        public String Cartshow(HttpServletRequest request, HttpServletResponse response){
            //获取session域中的对象
            User user = (User) request.getSession().getAttribute("loginUser");
            int uid = user.getId();
            //1. 获取session会话中当前用户的id
            if(user==null){
                //用户未登录, 从新登录
                return "redirect:/login.jsp";
            }
            //2. 根据用户id调用service查询这个用户的购物车数据
            Administratorimpl administratorimpl = new Administratorimpl(new userimpl());
            List<Cart> list = administratorimpl.findCartListByUid(uid);
            //3. 将用户购物车的数据放入request域中, 带到页面中进行展示
            request.setAttribute("list", list);
            //4. 跳转到购物车列表页面展示
            return  "forward:/cart.jsp";

            //cart.jsp
        }
        public String update(HttpServletRequest request,HttpServletResponse response){
            //1. 从会话中获取当前登录用户的id, 如果获取不到跳转到登录页面
            User user = (User) request.getSession().getAttribute("loginUser");
            if (user==null){
                //用户未登录, 从新登录
                return "redirect:/login.jsp";
            }
            //2. 获取购物项主键id参数
            String cid = request.getParameter("cid");
            //获取购买数量
            String cnum = request.getParameter("cnum");
            //3. 调用service, 执行修改业务
            Administratorimpl administratorimpl = new Administratorimpl(new userimpl());
            administratorimpl.updateCart(Integer.parseInt(cid), Integer.parseInt(cnum));
            //4. 跳转到购物车列表方法, 查看购物车中最新的数据展示
            return "redirect:/cart?method=Cartshow";
        }

        public String delete(HttpServletRequest request,HttpServletResponse response){
            //从会话中调用回话对象
            User user = (User)request.getSession().getAttribute("loginUser");
            if(user==null){
                //如果没有获取用户对象那么  跳转到登录界面
                return "redirect:/login.jsp";
            }
            //获取前端传来的购物车id
            String cid = request.getParameter("cid");
            //调用业务层对购物车数据进行删除
            Administratorimpl administratorimpl = new Administratorimpl(new userimpl());
            administratorimpl.deleteCart(Integer.parseInt(cid));
            return "redirect:/cart?method=Cartshow";

        }


}